package com.aier.admin_new.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin_new.entity.SchoolMasterEmail;
import com.aier.admin_new.entity.SchoolMasterEmailDetail;

public class SchoolMasterEmailDetailRowMapper implements RowMapper<SchoolMasterEmailDetail> {


	public SchoolMasterEmailDetailRowMapper() {
		super();
	}

	@Override
	public SchoolMasterEmailDetail mapRow(ResultSet arg0, int arg1){
		
		return parseHome( new SchoolMasterEmailDetail() ,arg0);
	}

	private SchoolMasterEmailDetail parseHome(SchoolMasterEmailDetail schoolMasterEmail, ResultSet rs) {
		
		return parseHome(schoolMasterEmail,rs,null);
	}

	private SchoolMasterEmailDetail parseHome(SchoolMasterEmailDetail sme,ResultSet rs, String su) {
		  try {
			  if (StringUtil.isStringNull(su)) {
		            su = "";
		        } else {
		            su += ".";
		        }
			  	sme.setSmedid(rs.getInt(su+"smedid"));
				sme.setSuid(rs.getInt(su+"suid"));
				sme.setSid(rs.getInt(su+"sid"));
				sme.setCdate(rs.getTimestamp(su+"cdate"));
				sme.setContent(rs.getString(su+"content"));
				sme.setSmeid(rs.getInt(su+"smeid"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return sme;
	}
	
	 public static Map<String, Object> getPar(Map<String, Object> par, SchoolMasterEmailDetail h) {
	        par.put("smedid",h.getSmedid());
	        par.put("suid",h.getSuid());
	        par.put("sid",h.getSid());
	        par.put("cdate",h.getCdate());
	        par.put("content",h.getContent());
	        par.put("smeid",h.getSmeid());
	        return par;
	    }
	  public static Map<String, Object> getArgs(Map<String, Object> args,SchoolMasterEmailDetail h){
		  args.put("smedid", h.getSmedid());
		  return args;
	  }
	  
	  /**
	     * 
	     * 
	     * @param tfp
	     * @return
	     */
	    public static PreparedStatementCreator getPreparedStatementCreator(
	            final SchoolMasterEmailDetail h) {
	        return new PreparedStatementCreator() {
	            @Override
	            public PreparedStatement createPreparedStatement(Connection con)
	                throws SQLException {
	                PreparedStatement ps = con.prepareStatement(parseSQL(h),
	                        new String[] { "smedid" });
	                parsePS(ps, h);
	                return ps;
	            }

	        };
	    }

	    private static String parseSQL(SchoolMasterEmailDetail h) throws SQLException {
	        StringBuffer sql = new StringBuffer("insert into school_master_email_detail (");
	        int count = 0;
	        
	        if (null != h.getSuid()) {
	            sql.append("suid,");
	            count++;
	        }
	        if (null != h.getSid()) {
	            sql.append("sid,");
	            count++;
	        }
	        if (null != h.getContent()) {
	            sql.append("content,");
	            count++;
	        }
	        if (null != h.getCdate()) {
	            sql.append("cdate,");
	            count++;
	        }
	        
	        if (null != h.getSmeid()) {
	            sql.append("smeid,");
	            count++;
	        }
	        
	        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
	        sql.append(") value(");
	        for (int i = 0; i < (count - 1); i++) {
	            sql.append("?,");
	        }
	        return sql.append("?)").toString();
	    }

	    private static void parsePS(PreparedStatement ps, SchoolMasterEmailDetail h)
	        throws SQLException {
	        int count = 1;
	        if (null != h.getSuid()) {
	            ps.setInt(count++,h.getSuid());
	        }
	        if (null != h.getSid()) {
	            ps.setInt(count++,h.getSid());
	        }
	        if (null != h.getContent()) {
	            ps.setString(count++,h.getContent() );
	        }
	        if (null != h.getCdate()) {
	            ps.setTimestamp(count++,h.getCdate());
	        }
	        if (null != h.getSmeid()) {
	            ps.setInt(count++,h.getSmeid() );
	        }
	    }
}